//
//  CYGHotsViewController.m
//  CYGHuntForCity
//
//  Created by cygarm on 16/2/20.
//  Copyright © 2016年 cygarm. All rights reserved.
//

#import "CYGHotsViewController.h"
#import "UIViewController+CYGPushedNaviBar.h"
#import "CYGHomeRcmdHeaderView.h"
#import "CYGHotsSelectionView.h"
#import "CYGHotsBlurView.h"
#import "CYGHomeRcmdCell.h"
#import "CYGHomeRcmdCellModel.h"
#import "CYGDetailViewController.h"
#import "CYGRefreshGifHeader.h"
#import <MJExtension.h>
#import "UIImage+Size.h"

@interface CYGHotsViewController () <CYGHotsSelectionViewDelegate, CYGHotsBlurViewDelegate>

@property (strong, nonatomic) NSMutableArray<CYGHomeRcmdCellModel *> *datas;
@property (strong, nonatomic) CYGHotsSelectionView *selectionView;
@property (strong, nonatomic) CYGHotsBlurView *blurView;

@end

@implementation CYGHotsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
	[self setupUI];
	[self setupRefreshHeader];
}

- (void)viewWillLayoutSubviews {
	CGRect frame = {{20, 10}, {CYGAppWidth-40, 40}};
	self.selectionView.frame = frame;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray<CYGHomeRcmdCellModel *> *)datas {
	if (_datas == nil) {
		NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hotsDatas" ofType:@"plist"]];
		_datas = [CYGHomeRcmdCellModel mj_objectArrayWithKeyValuesArray:array];
	}
	
	return _datas;
}

#pragma mark - UI

- (void)setupUI {
	[self cyg_initPushedNavigationBarWithBackgroundColor:_barColor titile:_barTitle subTitle:_barSubTitle];
	self.view.backgroundColor = CYGBackgroundColor;
	
	self.selectionView = [[CYGHotsSelectionView alloc] init];
	UIButton *btn1 = [self.selectionView viewWithTag:CYGHotsSelectionViewTagBtn1];
	UIButton *btn2 = [self.selectionView viewWithTag:CYGHotsSelectionViewTagBtn2];
	UIButton *btn3 = [self.selectionView viewWithTag:CYGHotsSelectionViewTagBtn3];
	UIButton *btn4 = [self.selectionView viewWithTag:CYGHotsSelectionViewTagBtn4];
	[btn1 setTitle:@"分类" forState:UIControlStateNormal];
	[btn2 setTitle:@"地区" forState:UIControlStateNormal];
	[btn3 setTitle:@"排序" forState:UIControlStateNormal];
	[btn4 setTitle:@"地图" forState:UIControlStateNormal];
	[self.selectionView.listBtn setTitle:@"列表" forState:UIControlStateNormal];
	self.selectionView.delegate = self;
	self.selectionView.listBtn.alpha = 0;
	[self.tableView addSubview:self.selectionView];
}

- (void)setupRefreshHeader {
	CYGRefreshGifHeader *header = [CYGRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeaderLoadNewData)];
	header.lastUpdatedTimeLabel.hidden = YES;
	header.stateLabel.hidden = YES;
	[header beginRefreshing];
	
	self.tableView.mj_header = header;
}

- (void)refreshHeaderLoadNewData {
	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
		[self.tableView.mj_header endRefreshing];
	});
}

- (CYGHotsBlurView *)blurView {
	if (_blurView == nil) {
		_blurView = [CYGHotsBlurView blurViewWithImage:[UIImage imageWithCaputureView:self.tableView]];
		_blurView.frame = CGRectMake(0, 0, CYGAppWidth, CYGAppHeight-64);
		_blurView.delegate = self;
	}
	
	return _blurView;
}

#pragma mark - IBAction


#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	
}

#pragma mark - Table view data source & delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 210.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	CYGHomeRcmdCellModel *model = self.datas[indexPath.row];
	CYGHomeRcmdCell *cell = [CYGHomeRcmdCell cellWithTableView:tableView model:model];
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	CYGDetailViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"idDetailVC"];; 
	[self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
	[UIView animateWithDuration:0.3 animations:^{
		self.selectionView.alpha = scrollView.contentOffset.y < 0 ? 0 : 1;
	}];
}

#pragma mark - CYGHotsSelectionViewDelegate
- (void)hotsSelectionViewBtnClick:(UIButton *)btn {
	CYGHotsSelectionViewTag btnTag = btn.tag;
	
	switch (btnTag) {
		case CYGHotsSelectionViewTagBtn1:
		case CYGHotsSelectionViewTagBtn2:
		case CYGHotsSelectionViewTagBtn3: {
			if (btn.selected) {
				[self showBlurView];
			} else {
				[self hideBlurView];
			}
			break;
		}
		case CYGHotsSelectionViewTagBtn4: {
			[UIView animateWithDuration:0.3 animations:^{
				self.selectionView.bigView.alpha = 0;
				self.selectionView.listBtn.alpha = CYGHotsSelectionViewAlpha;
			}];
			break;
		}
		case CYGHotsSelectionViewTagBtnList: {
			[UIView animateWithDuration:0.3 animations:^{
				self.selectionView.bigView.alpha = CYGHotsSelectionViewAlpha;
				self.selectionView.listBtn.alpha = 0;
			}];
			break;
		}
		default: {
			break;
		}
	}
}

- (void)showBlurView {
	[self.tableView insertSubview:self.blurView belowSubview:self.selectionView];
	self.tableView.scrollEnabled = NO;
}

- (void)hideBlurView {
	[self.blurView removeFromSuperview];
	self.tableView.scrollEnabled = YES;
}

#pragma mark - CYGHotsBlurViewDelegate

- (void)CYGHotsBlurViewCancleBtnClick:(UIButton *)btn {
	[self.selectionView.selectedBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
}

@end
